package com.garlicts.excel;

import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;

public class CellStyleFactory {

	private Map<String, CellStyle> styleMap = new HashMap<String, CellStyle>();
	
	private Workbook wb;
	
	private static final String style_normal = "normal";
	
	private static final String style_error = "error";
	
	public CellStyleFactory(Workbook wb) {
		this.wb = wb;
		init();
	}
	
	private void init() {
		
		CellStyle normalStyle = createNormal();
		styleMap.put(style_normal, normalStyle);
		
		CellStyle errorStyle = createError();
		styleMap.put(style_error, errorStyle);
		
	}
	
	public CellStyle createNormal() {
		
		CellStyle cellStyle = this.wb.createCellStyle();
		cellStyle.setAlignment(HorizontalAlignment.LEFT);
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		
		Font font = wb.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short)12);
		cellStyle.setFont(font);
		
		return cellStyle;
		
	}
	
	public CellStyle createError() {
		
		CellStyle cellStyle = this.wb.createCellStyle();
		cellStyle.setAlignment(HorizontalAlignment.LEFT);
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		
		cellStyle.setFillForegroundColor(HSSFColor.RED.index);
		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		
		Font font = wb.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short)12);
		cellStyle.setFont(font);
		
		return cellStyle;
		
	}
	
	public CellStyle getNormalStyle() {
		return styleMap.get(style_normal);
	}
	
	public CellStyle getErrorStyle() {
		return styleMap.get(style_error);
	}
	
	public Map<String, CellStyle> getStyleMap(){
		return styleMap;
	}
	
}